package com.zhava.crawler.infrastructure.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求头工具类
 *
 * @author zhaxiang
 */
public class HeadersUtil {

    /**
     * 获取默认的请求头信息
     *
     * @return 默认请求头Map
     */
    public static Map<String, String> getDefaultHeaders() {
        Map<String, String> defaultHeaders = new HashMap<>();
        defaultHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        defaultHeaders.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        defaultHeaders.put("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        defaultHeaders.put("Accept-Encoding", "gzip, deflate, br");
        defaultHeaders.put("Connection", "keep-alive");
        defaultHeaders.put("Cache-Control", "max-age=0");
        defaultHeaders.put("Upgrade-Insecure-Requests", "1");
        return defaultHeaders;
    }

    /**
     * 合并自定义请求头和默认请求头
     * 自定义请求头的值会覆盖默认请求头的值
     *
     * @param customHeaders 自定义请求头
     * @return 合并后的请求头
     */
    public static Map<String, String> mergeWithDefaultHeaders(Map<String, String> customHeaders) {
        Map<String, String> mergedHeaders = getDefaultHeaders();
        
        if (customHeaders != null && !customHeaders.isEmpty()) {
            mergedHeaders.putAll(customHeaders); // 用户自定义值会覆盖默认值
        }
        
        return mergedHeaders;
    }
} 